using System.Reflection.Metadata.Ecma335;
using System.Text.RegularExpressions;

namespace CSharpePro;

public class User
{
    static Dictionary<String, String> users = new Dictionary<String,String >();

    public static (bool, string) ValidateUsername(String username)
    {
        if (string.IsNullOrEmpty(username))
        {
            return (false, "用户名不能为空");
        }

        if (username.Length < 3 || username.Length > 15)
        {
            return (false, "用户名长度必须为3-15个字符！");
        }

        if (!Regex.IsMatch(username, @"^[a-zA-Z0-9]+$"))
        {
            return (false, "用户名只能包含字母，数字和下划线");
        }
        return (true, username);
    }

    public static (bool, string) ValidatePassword(String password, String confirmPassword)
    {
        if (string.IsNullOrWhiteSpace(password))
        {
            return (false, "密码不能为空");
        }

        if (password.Length < 6 || password.Length > 20)
        {
            return (false, "密码长度必须为6-20个字符");
        }

        if (!Regex.IsMatch(password, @"[A-Z]") || !Regex.IsMatch(password, @"[a-z]") || !Regex.IsMatch(password, @"[0-9]"))
        {
            return (false, "密码必须包含至少1个大写字母、1个小写字母和1个数字");
        }

        if (password != confirmPassword)
        {
            return (false, "两次密码不一致！");
        }
        return (true, password);
    }
    public void show()
    {
        while (true)
        {
            Console.WriteLine("请输入数字以选择功能\n");
            Console.WriteLine("1.登录 2.注册 3.退出");
            String input = Console.ReadLine();
            if (string.IsNullOrWhiteSpace(input))
            {
                Console.WriteLine("不可为空,请输入有效数字!!!");
                continue;
            }

            if (!int.TryParse(input, out int num))
            {
                Console.WriteLine(" 请输入有效数字");
                continue;
            }

            switch (num)
            {
                case 1:
                    Login();
                    break;
                case 2:
                    Regist();
                    break;
                case 3:
                    Logout();
                    break;
                default:
                    Console.WriteLine("请输入1-3之间的数字");
                    break;

            }
        }
    }

    public void Login()
    {
        Console.Write("====登录界面====\n1");
        Console.Write("请输入用户名：");
        String username = Console.ReadLine();
        if (!users.ContainsKey(username))
        {
            Console.WriteLine("用户名不存在");
            return;
        }
        Console.Write("请输入密码: ");
        String password = Console.ReadLine();
        if (users[username] != password)
        {
            Console.WriteLine("密码错误");
            return;
        }
        Console.WriteLine($"登陆成功！欢迎！{username}");
    }

    public void Regist()
    {
        Console.Write("====用户注册====\n");
        String username;
        while (true)
        {
            Console.WriteLine("请输入用户名：");
            username = Console.ReadLine();
            var (isValid, message) = ValidateUsername(username);
            if (!isValid)
            {
                Console.WriteLine($"{message}");
                continue;
            }

            if (users.ContainsKey(username))
            {
                Console.WriteLine("用户名已存在，请选择其他用户名");
                continue;
            }

            break;
        }

        string password;
        while (true)
        {
            Console.WriteLine("请输入密码：");
            password = Console.ReadLine();
            Console.WriteLine("请确认密码:");
            string confirmPassword = Console.ReadLine();
            var(isValid,message)=ValidatePassword(password, confirmPassword);
            if (!isValid)
            {
                Console.WriteLine($"{message}");
                continue;
            }
            break;
        }
        users.Add(username, password);
        Console.WriteLine("注册成功!");
    }

    public void Logout()
    {
        Console.WriteLine("感谢使用，再见！");
        return;
    }
}